﻿Imports sonicERPDAL
Imports sonicERPinfo

Public Class ClienteLista
    Inherits Paginabase
#Region "Variaveis Globais"
    Dim lista As New List(Of ClienteInfo)

    Private Property CurrentPage() As Integer
        Get
            Return Integer.Parse((ViewState("CurrentPage")))
        End Get
        Set(ByVal value As Integer)
            ViewState("CurrentPage") = value
        End Set
    End Property

    Private Property PageCount() As Integer
        Get
            Return Integer.Parse((ViewState("PageCount")))
        End Get
        Set(ByVal value As Integer)
            ViewState("PageCount") = value
        End Set
    End Property

#End Region
    
    '#Region "Eventos do Form"
    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Response.Redirect(ResolveUrl("~/adm/CadastroCliente.aspx"))
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            pnl1.Visible = False
            If Not IsPostBack Then

                Dim paginavisel As Boolean
                paginavisel = New Util().validarpagina(9)
                If Not paginavisel Then
                    FormsAuthentication.SignOut()
                    Response.Redirect(ResolveUrl("~/Login.aspx"))
                End If
                CurrentPage = 0
                tipocliente()
                getcliente()

            End If
        Catch ex As Exception
            pnl1.Visible = True
            lblerro.Text = ex.Message
        End Try
    End Sub
    '    Protected Sub gvList_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvList.RowCommand
    '        Try
    '            pnl1.Visible = False

    '            If e.CommandName = "Editar" Then
    '                Dim i As Integer = Convert.ToString(e.CommandArgument)
    '                Dim row As GridViewRow = gvList.Rows(i)

    '                Dim url As String = "~/adm/CadastroCliente.aspx?id=" + Server.HtmlEncode(row.Cells(1).Text)

    '                Response.Redirect(ResolveUrl(url))
    '            End If
    '        Catch ex As Exception
    '            lblerro.Text = ex.Message
    '            pnl1.Visible = True
    '        End Try
    '    End Sub


    '    Protected Sub gvList_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvList.PageIndexChanging
    '        Try
    '            pnl1.Visible = False
    '            e.Cancel = True
    '            gvList.PageIndex = e.NewPageIndex
    '            getcliente()
    '        Catch ex As Exception
    '            pnl1.Visible = True
    '            lblerro.Text = ex.Message
    '        End Try

    '    End Sub
    Protected Sub btnimprimir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnimprimir.Click
        Dim tipocliente As String = ""

        If ddltipoCliente.SelectedValue <> "0" Then
            tipocliente = ddltipoCliente.SelectedItem.Text

        End If
        Dim javascript As String = "<script language ='javascript'>"
        javascript += " window.open('Imprimir.aspx?idrelatorio=0&nome=" + Server.HtmlDecode(txtcliente.text) + "&dtinicio=" + Server.HtmlDecode(txtdataininico.Text) + "" +
      "&dtfim=" + Server.HtmlDecode(txtdatafim.Text) + "&tipo=" + Server.HtmlDecode(tipocliente) + " ', '', 'letf=0,top=0,width=900px,height=900px,toolbar=0,scrollbars=1,status=0');"
        javascript += " </script>"
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "imprimir", javascript)

    End Sub

    Protected Sub btnok_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnok.Click
        Try
            pnl1.Visible = False

            If txtdataininico.Text = "" OrElse txtdatafim.Text = "" Then
                txtdataininico.BackColor = Drawing.Color.White
                txtdatafim.BackColor = Drawing.Color.White
            Else


                Dim dtinicio As DateTime
                Dim dtfim As DateTime
                DateTime.TryParseExact(txtdataininico.Text, "dd/MM/yyyy", Nothing, Nothing, dtinicio)
                DateTime.TryParseExact(txtdatafim.Text, "dd/MM/yyyy", Nothing, Nothing, dtfim)
                If dtinicio = DateTime.MinValue Then
                    txtdataininico.Focus()
                    txtdataininico.BackColor = Drawing.Color.Aqua
                    txtdatafim.BackColor = Drawing.Color.White
                    Throw New Exception("data invalida digite a data no formato ex: dd/mm/yyyy")
                Else
                    txtdataininico.BackColor = Drawing.Color.White
                End If

                If dtfim = DateTime.MinValue Then
                    txtdatafim.Focus()
                    txtdatafim.BackColor = Drawing.Color.Aqua
                    txtdataininico.BackColor = Drawing.Color.White
                    Throw New Exception("data invalida digite a data no formato ex: dd/mm/yyyy")
                Else
                    txtdatafim.BackColor = Drawing.Color.White
                End If
            End If
            getcliente()
        Catch ex As Exception
            lblerro.Text = ex.Message
            pnl1.Visible = True

        End Try
    End Sub

    Public Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs)
      
        CurrentPage = 0
        getcliente()
    End Sub

    Public Sub btnLast_Click(ByVal sender As Object, ByVal e As EventArgs)
        CurrentPage -= 1
        If CurrentPage < 0 Then
            CurrentPage = 0
        End If
        getcliente()

    End Sub

    Public Sub btnPrevious_Click(ByVal sender As Object, ByVal e As EventArgs)
        CurrentPage = PageCount - 1
        getcliente()

    End Sub

    Public Sub btnFirst_Click(ByVal sender As Object, ByVal e As EventArgs)
        CurrentPage += 1
        If CurrentPage > (PageCount - 1) Then
            CurrentPage = PageCount - 1
        End If
        getcliente()

    End Sub


    
    '#End Region

    '#Region "Procedimentos e Funções"

    Public Sub tipocliente()

        ddltipoCliente.DataSource = dal.cliente.GetTipoCliente(New ClienteInfo)
        ddltipoCliente.DataTextField = "tipocliente"
        ddltipoCliente.DataValueField = "id"
        ddltipoCliente.DataBind()
        ddltipoCliente.Items.Insert(0, "Selecione")

    End Sub

    Public Sub getcliente()
        Try

            pnl1.Visible = False
            If Not String.IsNullOrEmpty(txtcliente.Text) Then

                sessaousuario.cliente.nomecliente = Convert.ToString(txtcliente.Text)
            Else
                sessaousuario.cliente.nomecliente = String.Empty
            End If

            If ddltipoCliente.SelectedIndex <> 0 Then

                sessaousuario.cliente.tipocliente = Convert.ToString(ddltipoCliente.SelectedItem.Text)
            Else
                sessaousuario.cliente.tipocliente = String.Empty
            End If


            If txtdataininico.Text = String.Empty Then

                sessaousuario.cliente.dtinicio = Now.AddMonths(-2)
                txtdataininico.Text = Now.AddMonths(-2).Date
            Else

                sessaousuario.cliente.dtinicio = Convert.ToDateTime(txtdataininico.Text)
            End If
            If txtdatafim.Text = "" Then
                sessaousuario.cliente.dt_fim = Now
                txtdatafim.Text = Now.Date
            Else
                sessaousuario.cliente.dt_fim = Convert.ToDateTime(txtdatafim.Text)
            End If
            Dim pageds As New PagedDataSource()
            pageds.AllowPaging = True
            pageds.PageSize = 5
            lista = dal.cliente.Getlista(sessaousuario)
            pageds.DataSource = lista
            If lista.Count <= 5 Then
                CurrentPage = 0
            End If
            pageds.CurrentPageIndex = CurrentPage

            PageCount = pageds.PageCount

            rptCliente.DataSource = pageds
            rptCliente.DataBind()

            btnPrevious.Enabled = Not pageds.IsLastPage
            btnNext.Enabled = Not pageds.IsFirstPage

            lblPageCount.Text = PageCount.ToString()
            lblSelectedPage.Text = Convert.ToString((CurrentPage + 1))

            If (Integer.Parse(PageCount.ToString()) <= 1) Then
                pnlpaginacao.Visible = False
            Else
                pnlpaginacao.Visible = True


            End If
            If lista.Count.Equals(0) Then
                rptCliente.Visible = False
            Else
                rptCliente.Visible = True
            End If

        Catch ex As Exception
            pnl1.Visible = True
            lblerro.Text = ex.Message
        End Try
    End Sub

    '#End Region




    Private Sub rptCliente_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCliente.ItemDataBound
        Dim ddltelefone As DropDownList

        Dim pagina As Integer = 0
        pagina = CurrentPage * 5

        For j = 0 To rptCliente.Items.Count - 1

            ddltelefone = CType(rptCliente.Items(j).FindControl("ddltelefone"), DropDownList)
            If (Not ddltelefone Is Nothing) Then
                sessaousuario.cliente.telefones = New List(Of TelefoneInfo)
                If (pagina > 0) Then
                    sessaousuario.cliente.telefones = lista(pagina + j).telefones
                Else
                    sessaousuario.cliente.telefones = lista(j).telefones
                End If


                ddltelefone.DataSource = sessaousuario.cliente.telefones
                ddltelefone.DataValueField = "idtelefone"
                ddltelefone.DataTextField = "telefone"
                ddltelefone.DataBind()

            End If


        Next


    End Sub

    
    Protected Sub rptCliente_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptCliente.ItemCommand
        If e.CommandName = "Editar" Then
            Response.Redirect(ResolveUrl("~/adm/CadastroCliente.aspx?id=" + e.CommandArgument + ""))
        End If
    End Sub

End Class